<?php

/**
 *    本期热卖商品管理控制器
 *
 *    @author   yanghj 2015.02.28
 *    @usage    none
 */
class HotgoodsApp extends BackendApp
{
    var $_hotgoods_mod;

    function __construct()
    {
        $this->HotgoodsApp();
    }

    function HotgoodsApp()
    {
        parent::BackendApp();

        $this->_hotgoods_mod =& m('hotgoods');
    }

    /**
     *    管理
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
    function index()
    {

        //更新排序
        if (isset($_GET['sort']) && isset($_GET['order']))
        {
            $sort = strtolower(trim($_GET['sort']));
            $order = strtolower(trim($_GET['order']));
            if (!in_array($order, array('asc', 'desc')))
            {
                $sort = 'sort_order,id';
                $order = 'desc';
            }
        }
        else
        {
            $sort = 'sort_order,id';
            $order = 'desc';
        }
        $page   =   $this->_get_page(10);    //获取分页信息
        $hotgoods = $this->_hotgoods_mod->find(array(
            'limit'         => $page['limit'],  //获取当前页的数据
            'order' => "$sort $order",
            'count'         => true             //允许统计
        ));
        $recommendstore_mod = & m('recommendstore');
        $recommend_store = $recommendstore_mod->get_list();
        $page['item_count'] = $this->_hotgoods_mod->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->import_resource(array('script' => 'inline_edit.js'));
        $this->assign('page_info', $page);          //将分页信息传递给视图，用于形成分页条
        $this->assign('hotgoods', $hotgoods);
        $this->assign('recommend_store', $recommend_store);
        $this->display('hotgoods.index.html');
    }
    /**
     *    新增
     *
     *    @author    Garbin
     *    @return    void
     */
    function add()
    {
        if (!IS_POST)
        {
            $hotgoods = array(
            'sort_order'    => '255',
            'link'          => 'http://',
            );
			
            $this->assign('hotgoods' , $hotgoods);
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display('hotgoods.form.html');
        }
        else
        {

            $data = array();
            $data['title']      =   html_script(trim($_POST['title']));
            $data['link']  =   html_script(trim($_POST['link']));
            $data['sort_order']      =   intval($_POST['sort_order']);
            if (!$this->_hotgoods_mod->add($data))
            {
                $this->show_warning($this->_hotgoods_mod->get_error());

                return;
            }

            $this->_hotgoods_mod->clear_cache();//清除缓存
            $this->admin_log('添加本期热卖商品:' . $data['title']);
            $this->show_message('添加成功',
                'back_list',    'index.php?app=hotgoods',
                '继续添加', 'index.php?app=hotgoods&amp;act=add'
            );
        }
    }

    /**
     *    编辑
     *
     *    @author    Garbin
     *    @return    void
     */
    function edit()
    {	
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$id)
        {
            $this->show_warning('没有相关的数据');

            return;
        }
        if (!IS_POST)
        {
            $find_data     = $this->_hotgoods_mod->find($id);
            if (empty($find_data))
            {
                $this->show_warning('没有相关的数据');

                return;
            }
            $hotgoods    =   current($find_data);
            $this->assign('hotgoods' , $hotgoods);
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display('hotgoods.form.html');
        }
        else
        {
            $data = array();
            $data['title']      =   html_script(trim($_POST['title']));
            $data['link']  =   html_script(trim($_POST['link']));
            $data['sort_order']      =   intval($_POST['sort_order']);
            $this->_hotgoods_mod->edit($id, $data);
            if ($this->_hotgoods_mod->has_error())    //有错误
            {
                $this->show_warning($this->_hotgoods_mod->get_error());

                return;
            }
            $this->_hotgoods_mod->clear_cache();//清除缓存
            $this->admin_log('修改本期热卖商品:' . $data['title']);
            $this->show_message('编辑成功',
                'back_list',     'index.php?app=hotgoods',
                '再次编辑', 'index.php?app=hotgoods&amp;act=edit&amp;id=' . $id);
        }
    }

    //异步修改数据
   function ajax_col()  
   {
       $id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
       $column = empty($_GET['column']) ? '' : trim($_GET['column']);
       $value  = isset($_GET['value']) ? trim($_GET['value']) : '';
       $data   = array();

       if (in_array($column ,array('title', 'is_recommend', 'is_hot', 'sort_order')))
       {
           $data[$column] = $value;
           $this->_hotgoods_mod->edit($id, $data);
           if(!$this->_hotgoods_mod->has_error())
           {
               $this->_hotgoods_mod->clear_cache();//清除缓存
               echo ecm_json_encode(true);
           }
       }
       else
       {
           return ;
       }
       return ;
   }

    function drop()
    {
        $id = isset($_GET['id']) ? trim($_GET['id']) : 0;
        if (!$id)
        {
            $this->show_warning('请选择要删除的数据');

            return;
        }
        $ids = explode(',', $id);//获取一个类似array(1, 2, 3)的数组
        $this->admin_log('删除本期热卖商品:' . $id);
        if (!$this->_hotgoods_mod->drop($ids))    //删除
        {
            $this->show_warning($this->_hotgoods_mod->get_error());

            return;
        }
        $this->_hotgoods_mod->clear_cache();//清除缓存
        $this->show_message('删除成功');
    }

     /* 更新排序 */
    function update_order()
    {
        if (empty($_GET['id']))
        {
            $this->show_warning('Hacking Attempt');
            return;
        }

        $ids = explode(',', $_GET['id']);
        $sort_orders = explode(',', $_GET['sort_order']);
        foreach ($ids as $key => $id)
        {
            $this->_hotgoods_mod->edit($id, array('sort_order' => $sort_orders[$key]));
        }
        $this->_hotgoods_mod->clear_cache();//清除缓存
        $this->show_message('更新排序成功');
    }



}

?>